<?php
namespace Tyrone\Model\Game;
use Tyrone\Model\Base;
use Medoo\Medoo;

class Role extends Base
{
    protected $table = 'role';

    public function  getCurrentOnline(array $map): array
    {

        $res = $this->database->select($this->table, [
            'pf(platform)',
            'num' =>Medoo::raw('COUNT(*)'),
            'ip_num' => Medoo::raw('COUNT(DISTINCT last_login_ip)'),
        ], $map + [
            'GROUP' => ['pf']
            ]
        );
        return array_column($res, null, 'platform');

    }


    public function getRoleList($map)
    {
        $alias = [
            'pf' =>'platform',
            'zone_id' =>['server_id'],
            'account' =>'username',
            'id' =>'player_id',
            'name' =>'name',
            'sex' =>'sex',
            'lev' =>'level',
            'fc' =>'power',
            'last_login_time' =>'login_time',
            'reg_time' =>'reg_time',
            'last_login_ip' =>'login_ip',
            'state' =>'status',
        ];
        $columns = ['id'];
        foreach ($alias as $column => $alia){
            if(!is_array($alia)){
                $alia = [$alia];
            }
            foreach ($alia as $key){
                $columns[] = "$column({$key})";
            }
        }

        $columns['login_date'] = Medoo::raw('FROM_UNIXTIME(last_login_time,\'%Y-%d-%m\')');
        $list = $this->database->select($this->table,$columns, $map);
        return $list;

    }

}